from xml.dom.minidom import parse
import pandas as pd
from glob import glob


images_path = './xx'
pts_postfix =  '.xml'
pts011 = glob(f'{images_path}/*{pts_postfix}')  ##list

target_all = pd.DataFrame()

for xxx in pts011:
    domTree = parse(xxx)
    rootNode = domTree.documentElement
    # 所有小框
    customers = rootNode.getElementsByTagName("object")
    data_all = []
    for node in customers:
        node_ = node.getElementsByTagName('name')
        name = node_[0].childNodes[0].data  ## .data
        node_ = node.getElementsByTagName('xmin')
        xmin = node_[0].childNodes[0].data
        node_ = node.getElementsByTagName('ymin')
        ymin = node_[0].childNodes[0].data
        node_ = node.getElementsByTagName('xmax')
        xmax = node_[0].childNodes[0].data
        node_ = node.getElementsByTagName('ymax')
        ymax = node_[0].childNodes[0].data
        data_all.append([name,xmin, ymin,xmax,ymax])
    data_df = pd.DataFrame(data_all,columns = ['name','xmin','ymin','xmax','ymax' ])
    hh = data_df.name.value_counts()
    data_temp = pd.DataFrame()
    data_temp['name'] = hh.index
    data_temp['val'] = hh.values
    data_temp['leng'] = data_temp['name'].map(lambda x: len(x))
    target = data_temp[data_temp.val != data_temp.leng ]
    target['file'] = xxx
    target_all = pd.concat([target_all,target],ignore_index = True)






if len(target_all) == 0:
    print('数据匹配成功!')
    images_path = './xx'
    pts_postfix =  '.xml'
    pts011 = glob(f'{images_path}/*{pts_postfix}')  ##list

    transfer_all = pd.DataFrame()

    for xxx in pts011:
        domTree = parse(xxx)
        rootNode = domTree.documentElement
        # 所有小框
        customers = rootNode.getElementsByTagName("object")
        data_all = []
        for node in customers:
            node_ = node.getElementsByTagName('name')
            name = node_[0].childNodes[0].data  ## .data
            node_ = node.getElementsByTagName('xmin')
            xmin = node_[0].childNodes[0].data
            node_ = node.getElementsByTagName('ymin')
            ymin = node_[0].childNodes[0].data
            node_ = node.getElementsByTagName('xmax')
            xmax = node_[0].childNodes[0].data
            node_ = node.getElementsByTagName('ymax')
            ymax = node_[0].childNodes[0].data
            data_all.append([xmin, ymin, xmax, ymin, xmax,ymax, xmin, ymax, name])

        temp = './data/txt/'+ xxx.split('\\')[-1].split('.')[0] + '.txt'
        f = open(temp, 'w', encoding='utf-8')
        for yyy in data_all:
            ii = ','.join(yyy) + "\n"
            f.writelines(ii)
        f.close()


        # data_df = pd.DataFrame(data_all,columns = ['xmin', 'ymin', 'xmax', 'ymin', 'xmax','ymax', 'xmin', 'ymax', 'name'])
        # temp = './data/txt/'+ xxx.split('\\')[-1].split('.')[0] + '.txt'
        # data_df.to_csv(temp,index=False,header=False,sep=',',quoting=0)






